//
//  MSFoodPhotosView.m
//  MSJD
//
//  Created by xiangbo on 2018/9/29.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSFoodPhotosView.h"
#import "MSCollectionBaseCell.h"
#import "YYPhotoGroupView.h"

@interface MSFoodPhotosView ()<UICollectionViewDelegate, UICollectionViewDataSource,MSDetailViewDelegate>

@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)MSFoodDetailData *detailData;

@end

@implementation MSFoodPhotosView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"特色相册";
        titleLabel.textColor = UIColorHex(0x444444);
        titleLabel.font = [UIFont boldSystemFontOfSize:16.f];
        
        UIView *spaceView = [UIView new];
        spaceView.backgroundColor = UIColorHex(0xf2f2f2);
        [self addSubview:spaceView];
        
        [self addSubview:titleLabel];
        [self addSubview:self.collectionView];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.left.equalTo(self).offset(15);
            make.right.equalTo(self).offset(-15);
            make.height.mas_equalTo(58);
        }];
        
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLabel.mas_bottom);
            make.left.right.equalTo(self);
        }];
        
        [spaceView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.collectionView.mas_bottom);
            make.left.right.bottom.equalTo(self);
            make.height.mas_equalTo(10);
        }];
    }
    return self;
}

- (void)configData:(id)data indexPath:(NSIndexPath *)indexPath{
    self.detailData = (MSFoodDetailData *)data;
    [self.collectionView reloadData];
    
}
#pragma mark UITableViewDelegate and DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.detailData.photos.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MSPhotosCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(MSPhotosCollectionViewCell.class) forIndexPath:indexPath];
    [cell.image setImageWithURL:[NSURL URLWithString:self.detailData.photos[indexPath.row]] options:YYWebImageOptionSetImageWithFadeAnimation];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray *items = [NSMutableArray array];
    UIView *fromView = nil;
    for (int index = 0; index < self.detailData.photos.count; index++) {
        MSPhotosCollectionViewCell *currentCell = (MSPhotosCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        UIImageView *imageView = currentCell.image;;
        YYPhotoGroupItem *item = [YYPhotoGroupItem new];
        item.thumbView = imageView;
        item.largeImageURL = [NSURL URLWithString:self.detailData.photos[index]];
        [items addObject:item];

        if (index == indexPath.row)
            fromView = imageView;
    }
    YYPhotoGroupView *photoGroupView = [[YYPhotoGroupView alloc] initWithGroupItems:items];
    photoGroupView.blurEffectBackground = YES;
    photoGroupView.autoHidePager = NO;
    photoGroupView.usePageLabel = YES;
    photoGroupView.clipToBoundsAnimate = YES;
    [photoGroupView presentFromImageView:fromView toContainer:UIApplication.sharedApplication.keyWindow fromItemIndex:indexPath.row animated:YES completion:nil];
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 15.f, 20.f, 15);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(134, 89);
}

//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
//    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
//
//    MSPhotosCollectionViewCell *photoCell = (MSPhotosCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    [MSImageFullScreenManager scanBigImageListWithImageUrlList:self.detailData.photos sourceImageView:photoCell.image selectIndex:indexPath.item];
//}

#pragma mark -Property
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 15;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.backgroundColor = [UIColor whiteColor];
        
        [_collectionView registerClass:[MSPhotosCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([MSPhotosCollectionViewCell class])];
        
    }
    return _collectionView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
